package com.lxz.datasource.task;


import com.lxz.datasource.entity.QualityTrade;
import com.lxz.datasource.entity.QualityTradeHistory;
import com.lxz.datasource.service.IQualityTradeHistoryService;
import com.lxz.datasource.service.IQualityTradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TestTask {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IQualityTradeHistoryService qualityTradeHistoryService;
    @Autowired
    private IQualityTradeService qualityTradeService;

    //每天凌晨1点执行一次：0 0 1 * * ?
    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void test() {

        List<QualityTradeHistory> qualityTradeHistoryList = new ArrayList<>();
        List<QualityTrade> qualityTradeList = qualityTradeService.findAll();
        if (qualityTradeList != null && qualityTradeList.size() > 0) {
            for (QualityTrade qualityTrade : qualityTradeList) {
                QualityTradeHistory qualityTradeHistory = new QualityTradeHistory();
                BeanUtils.copyProperties(qualityTrade, qualityTradeHistory);
                qualityTradeHistoryList.add(qualityTradeHistory);
            }
            qualityTradeHistoryService.saveAll(qualityTradeHistoryList);
            //这里就需要将原表数据清空
            qualityTradeService.deleteAll();
            logger.info("执行成功。。。");
        }


        System.out.println("定时器启动");
    }

}
